# -*- coding: utf-8 -*-
from application import app
from flask import request, jsonify, g

from app.interceptors.Error.Execute import \
    api_error_404,api_error_500, web_error_404, \
    web_error_500, admin_error_404, admin_error_500, \
    default_error_404, default_error_500

@app.errorhandler(404)
def error_404(e):
    # app.logger.error(e)
    # LogLibs.addErrorLog( str( e ) )
    # if "/api" in request.full_path:
    #     return jsonify(gene_response(code=404, msg="接口不存在"))
    # else :
    #     return None
    # return ops_render('error/error.html', {'status': 404, 'msg': '很抱歉！您访问的页面不存在'})
    path = request.full_path
    if app.config["ROUTE"]["route_api"] in path:
        return api_error_404(path)
    elif app.config["ROUTE"]["route_web"] in path:
        return web_error_404(path)
    elif app.config["ROUTE"]["route_admin"] in path:
        return admin_error_404(path)
    else:
        return default_error_404(path)


# @app.errorhandler(Exception)
def error_500(e):
    # target_url = request.url
    # referer_url = request.referrer
    # query_params = request.values.to_dict()
    #
    # if "/api" in request.full_path:
    #     customer_id = g.customer_info.id
    #     add_api_error_log_service(target_url, referer_url, query_params, str(e), customer_id)
    #     return jsonify(gene_response(code=-1, msg="很抱歉！服务器内部异常"))
    # # # LogLibs.addErrorLog( str( e ) )
    # # elif "/web" in request.full_path:
    # #     user_id = g.current_user.uid
    # #     add_web_error_log_service(target_url, referer_url, query_params, e, user_id)
    # #     return ops_render('error/error.html', {'status': 500, 'msg': '很抱歉！服务器内部异常'})
    # else :
    #     return None
    path = request.full_path
    if app.config["ROUTE"]["route_api"] in path:
        return api_error_500(path)
    elif app.config["ROUTE"]["route_web"] in path:
        return web_error_500(path)
    elif app.config["ROUTE"]["route_admin"] in path:
        return admin_error_500(path)
    else:
        return default_error_500(path)